package legumes.shared.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Id;

import com.googlecode.objectify.annotation.Serialized;

public class Stall implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id private long sellSessionDate;
	@Serialized private Menu menu;
	@Serialized private List<OnSaleProduct> onSaleProducts;
	@Serialized private List<Receipe> receipes;
	/**
	 * Errors or warnings occurred when this stall has been produced by the
	 * parsing of the merchant mail. This field is transient.
	 */
	@Serialized private List<String> importMisunderstoodLines;
	@Serialized private List<String> importMessages;

	private int nbMaxWholeMenu;
	private int nbMaxHalfMenu;
	
	public Stall(){
		importMisunderstoodLines = new ArrayList<String>();
		importMessages = new ArrayList<String>();
		menu = new Menu();
		onSaleProducts = new ArrayList<OnSaleProduct>();
		receipes = new ArrayList<Receipe>();
		nbMaxWholeMenu = 50;
		nbMaxHalfMenu = 50;
	}
	
	public int computeWholeMenuLeft(){
		//TODO SMA => Must compute nb basket bought
		return 0;
	}
	
	public void addOnSaleProduct(OnSaleProduct p_) {
		getOnSaleProducts().add(p_);
	}

	public void clear() {
		onSaleProducts.clear();
		receipes.clear();
		menu.clear();
		importMisunderstoodLines.clear();
	}

	public void setStall(Stall stall_) {
		clear();
		onSaleProducts.addAll(stall_.getOnSaleProducts());
		receipes.addAll(stall_.getReceipes());
		menu.setMenu(stall_.getMenu());
	}

	public void removeOnSaleProduct(OnSaleProduct p_) {
		getOnSaleProducts().remove(p_);
	}

	public List<String> getImportMisunderstoodLines() {
		return importMisunderstoodLines;
	}

	public void addImportMisunderstoodLine(String l_) {
		importMisunderstoodLines.add(l_);
	}

	public List<String> getImportMessages() {
		return importMessages;
	}

	public void addImportMessage(String l_) {
		importMessages.add(l_);
	}

	public long getSellSessionDate() {
		return sellSessionDate;
	}

	public void setSellSessionDate(long sellSessionDate_) {
		sellSessionDate = sellSessionDate_;
		if (getMenu() != null) {
			getMenu().setDate(sellSessionDate);
		}
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu_) {
		menu_.setDate(getSellSessionDate());
		menu = menu_;
	}

	public List<OnSaleProduct> getOnSaleProducts() {
		return onSaleProducts;
	}

	public void setOnSaleProducts(List<OnSaleProduct> onSaleProducts_) {
		onSaleProducts = onSaleProducts_;
	}

	public List<Receipe> getReceipes() {
		return receipes;
	}

	public void setReceipes(List<Receipe> receipes_) {
		receipes = receipes_;
	}

	public void addReceipe(Receipe r_) {
		getReceipes().add(r_);
	}

	public void removeReceipe(Receipe r_) {
		getReceipes().remove(r_);
	}

	public OnSaleProduct findOnSaleProduct(String productName_) {
		for (OnSaleProduct osp : getOnSaleProducts()) {
			if (osp.getProduct().getName().equals(productName_)) {
				return osp;
			}
		}
		return null;
	}

	public Product newProduct() {
		Product product = new Product();
		product.setName(newProductName());
		return product;
	}

	public String newProductName() {
		return "Nouveau produit " + System.currentTimeMillis();
	}

	public Stall copy() {
		Stall stall = new Stall();

		for (OnSaleProduct osp : getOnSaleProducts()) {
			stall.addOnSaleProduct(osp.copy());
		}

		Menu menu = new Menu();
		menu.setHalfPrice(getMenu().getHalfPrice());
		menu.setWholePrice(getMenu().getWholePrice());
		for (MenuItem mi : getMenu().getMenuWholeItems()) {
			MenuItem menuItem = new MenuItem();
//			if (mi.hasRealProduct()) {
				menuItem.setOnSaleProduct(stall.findOnSaleProduct(mi
						.getArticleName()));
//			} else {
//				menuItem.setMenuProduct(mi.getMenuProduct());
//			}
			menuItem.setQuantity(new Quantity(mi.getQuantity()));
			menu.addMenuWholeItem(menuItem);
		}
		stall.setMenu(menu);

		for (Receipe r : getReceipes()) {
			stall.addReceipe(r.copy());
		}

		return stall;
	}
	
	public int getNbMaxWholeMenu() {
		return nbMaxWholeMenu;
	}

	public void setNbMaxWholeMenu(int nbMaxWholeMenu) {
		this.nbMaxWholeMenu = nbMaxWholeMenu;
	}

	public int getNbMaxHalfMenu() {
		return nbMaxHalfMenu;
	}

	public void setNbMaxHalfMenu(int nbMaxHalfMenu) {
		this.nbMaxHalfMenu = nbMaxHalfMenu;
	}


	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[Stall").append(" date=").append(getSellSessionDate())
				.append(" onSaleProducts=");

		for (OnSaleProduct osp : getOnSaleProducts()) {
			sb.append(osp).append("\n");
		}

		sb.append(" \n\tmenu=").append(getMenu());

		sb.append(" \n\treceipes=");
		for (Receipe r : getReceipes()) {
			sb.append(r).append("\n");
		}

		sb.append("]");

		return sb.toString();
	}
}
